package com.yhg.qxmall.member.front;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yhg.common.utils.R;
import com.yhg.qxmall.member.entity.MemberEntity;
import com.yhg.qxmall.member.service.MemberReceiveAddressService;
import com.yhg.qxmall.member.service.MemberService;
import com.yhg.qxmall.member.utils.DateUtil;
import com.yhg.qxmall.member.utils.MD5Utils;
import com.yhg.qxmall.member.vo.MemberLoginVo;
import com.yhg.qxmall.member.vo.temp.MQMemberRegistVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.UUID;

/**
 * @author 凌月青缃
 * @date 2020/12/20
 *
****************   ===========    接口重写     ============   *****************  **/

@RestController
@RequestMapping("front/member")
public class FrontMemberController {
    @Autowired
    MemberService memberService;

    @Autowired
    MemberReceiveAddressService memberReceiveAddressService;


    @PostMapping("/login")
    public R loginTest(@RequestBody MemberLoginVo vo){
        MemberEntity memberEntity = memberService.loginFront(vo);

        if (memberEntity!=null){
            return R.ok().put("data",memberEntity);
        }else {
            return R.error().put("msg","用户名或密码错误！");
        }
    }

    @GetMapping("/sms/sendCode")
    public R sendCode(@RequestParam String phone){
        //校验手机号是否已经注册
        MemberEntity count = memberService.getOne(new QueryWrapper<MemberEntity>().eq("mobile", phone));
        if (count==null){
            //生成一个验证码,并与手机号一起存入数据库
            String code = UUID.randomUUID().toString().substring(0, 5);
            MemberEntity entity = new MemberEntity();
            entity.setAccessToken(code);
            entity.setMobile(phone);
            memberService.save(entity);
//        thirdPartFeignService.sendCode(phone,code);
            return R.ok().put("data",code);
        }else return R.error().put("msg","该手机已被注册！");

    }

    @PostMapping("/regist")
    public R regist(@RequestBody MQMemberRegistVo vo){
        //校验验证码
        String code = vo.getCode();
        String phone = vo.getPhone();

        MemberEntity memberEntity = memberService.getOne(new QueryWrapper<MemberEntity>().eq("mobile", phone));
       if( memberEntity.getAccessToken().equals(code)){
           //如果验证通过，则保存至数据库
           memberEntity.setUsername(vo.getUsername());
           String md5Code = MD5Utils.getMD5Code(vo.getPassword());
           memberEntity.setPassword(md5Code);
           memberEntity.setNickname("用户未设置");
           memberEntity.setBirth(new Date());
           memberEntity.setLevelId(1L);
           memberService.updateById(memberEntity);
           return  R.ok().put("msg","注册成功！");
       }else return R.error().put("msg","注册失败0");
    }


    /**
     * 回显用户信息接口
     */
//    @GetMapping
//    public  showMemberInfo(@RequestParam Long memberId){
//        //先
//        MemberEntity memberEntity = memberService.getById(memberId);
//        return memberEntity;
//    }
    /**
     * 新增收获地址
     */
//    @PostMapping
//    public R UpdateMemberInfo(@RequestBody Long memberId){
//
//    }



}
